<!--

-->
<template>
    <div class="GImageListPreview">
        <div v-for="item in list" :key="item.id" class="preview" :style="{width: `${width}px`, height: `${height}px`}">
            <img :src="imageFullPath(item.path)" class="image"/>
            <div class="cover">
                <div class="btn" @click="show(item)">查看</div>
            </div>
        </div>
    </div>
</template>

<script>
export default {
    name: "GImageListPreview",
    emits: ["update:modelValue"],
    props: {
        modelValue: {
            type: String,
            default: () => ""
        },
        src: {
            type: String,
            default: () => ""
        },
        height: {
            type: Number,
            default: () => 100
        },
        width: {
            type: Number,
            default: () => 100
        }
    },
    data() {
        return {
            list: []
        }
    },
    watch: {
        modelValue() {
            this.setListValue()
        },
        src() {
            this.setListValue()
        },
    },
    mounted() {
        this.setListValue();
    },
    methods: {
        setListValue() {
            if (this.modelValue) {
                const list = this.modelValue.split(";").filter(item => item).map(item => {
                    return {
                        path: item,
                        id: item
                    }
                })
                this.list = list;
            } else if (this.src) {
                const list = this.src.split(";").filter(item => item).map(item => {
                    return {
                        path: item,
                        id: item
                    }
                })
                this.list = list;
            }
        },
        imageFullPath(val) {
            const host = window.$config.baseUrl;
            return host + "/" + val;
        },

        show(item) {
            this.showBig(this.imageFullPath(item.path));
        },
        showBig(fullPath) {
            const width = 500;
            const height = 600;
            const left = (document.documentElement.clientWidth - width) / 2;
            window.open(fullPath, "", `width=${width}, height=${height}, left=${height}, top=160`)
        }
    }
}
</script>

<style lang="scss" scoped>
.GImageListPreview {
    display: flex;
    flex-direction: row;
    justify-content: flex-start;
    align-items: flex-start;
    flex-wrap: wrap;
    height: auto;
    .preview {
        width: 100px;
        height: 100px;
        border-radius: 6px;
        border: 1px solid #dcdfe6;
        margin-right: 10px;
        margin-bottom: 10px;
        position: relative;
        overflow: hidden;

        .image {
            width: 100%;
            height: 100%;
            object-fit: cover;
        }

        .cover {
            position: absolute;
            left: 0;
            top: 0;
            right: 0;
            bottom: 0;
            z-index: 1;
            display: flex;
            flex-direction: row;
            justify-content: center;
            align-items: center;
            background: rgba(0, 0, 0, 0);
            opacity: 0;

            .btn {
                font-size: 14px;
                color: var(--el-color-primary);
                width: 40px;
                height: 30px;
                display: flex;
                flex-direction: row;
                justify-content: center;
                align-items: center;
                cursor: pointer;

                &:active {
                    opacity: 0.8;
                }
            }

            .delete {
                color: var(--el-color-danger);
            }
        }

        &:hover {
            .cover {
                opacity: 1;
                background: rgba(0, 0, 0, 0.5);
            }
        }
    }
}
</style>
